package cn.cjxy.userms.controller;

import cn.cjxy.userms.entity.User;
import cn.cjxy.userms.service.UserService;
import cn.cjxy.userms.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/list")
public class ListServlet extends HttpServlet {
    //注入service层
    UserService userService=new UserServiceImpl();
    /**
     * 将查询出的用户列表信息，存入作用域，并跳转到list.jsp
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        //验证用户是否登录
        Object loginUser = session.getAttribute("loginUser");
        /*if (loginUser==null){
            //没有登录 回到登陆页面
            resp.sendRedirect(req.getContextPath()+"/login.jsp");
            //结束方法的运行
            return;
        }*/
        //查询所有的用户信息
        List<User> userList =userService.findAllUsers();
        //将用户列表信息存入作用域
        req.setAttribute("userList", userList);
        //跳转到列表页list.jsp
        req.getRequestDispatcher("/list.jsp").forward(req, resp);
    }
}
